package com.ruoyi.activieadmin.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.activieadmin.mapper.AaActiveVenueMapper;
import com.ruoyi.activieadmin.service.IAaActiveVenueService;
import com.ruoyi.activieadmin.domain.AaActiveVenue;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.activieadmin.domain.AaActive;
import com.ruoyi.campadmin.domain.CaVeueRentalObj;
import com.ruoyi.activieadmin.service.IAaActiveService;
import com.ruoyi.campadmin.service.ICaVeueRentalObjService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class AaActiveVenueServiceImpl implements IAaActiveVenueService {


    @Resource
    private AaActiveVenueMapper aaActiveVenueMapper;
    
    
    @Resource
    private IAaActiveService aaActiveService;
    @Resource
    private ICaVeueRentalObjService caVeueRentalObjService;

    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param aaActiveVenue 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<AaActiveVenue> getAaActiveVenuePage(PageDomain pageDomain, AaActiveVenue aaActiveVenue , AaActive aaActive , CaVeueRentalObj caVeueRentalObj) {
        if (Objects.isNull(aaActiveVenue)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<AaActiveVenue> queryWrapper = new LambdaQueryWrapper<>(new AaActiveVenue());
            //if(!Objects.isNull(aaActiveVenue.getName())){
            //	queryWrapper.like(AaActiveVenue::getName,aaActiveVenue.getName());
            //}
            

            if (!Objects.isNull(aaActiveVenue.getId())) {
                queryWrapper.eq(AaActiveVenue::getId, aaActiveVenue.getId());
            }

            if (!Objects.isNull(aaActiveVenue.getAaActiveId())) {
                queryWrapper.eq(AaActiveVenue::getAaActiveId, aaActiveVenue.getAaActiveId());
            }

            aaActive.setBaseEntityNull();
            aaActive.setId(null);
            if (!PageDomain.checkObjAllFieldsIsNull(aaActive)) {
                List<Long> idList = aaActiveService.getIdListByAaActive(aaActive);
                queryWrapper.in(AaActiveVenue::getAaActiveId, idList);
            }


            if (!Objects.isNull(aaActiveVenue.getCaVeueRentalObjId())) {
                queryWrapper.eq(AaActiveVenue::getCaVeueRentalObjId, aaActiveVenue.getCaVeueRentalObjId());
            }

            caVeueRentalObj.setBaseEntityNull();
            caVeueRentalObj.setId(null);
            if (!PageDomain.checkObjAllFieldsIsNull(caVeueRentalObj)) {
                List<Long> idList = caVeueRentalObjService.getIdListByCaVeueRentalObj(caVeueRentalObj);
                queryWrapper.in(AaActiveVenue::getCaVeueRentalObjId, idList);
            }


            if (!Objects.isNull(aaActiveVenue.getStatus())) {
                queryWrapper.like(AaActiveVenue::getStatus, aaActiveVenue.getStatus());
            }

            if (!Objects.isNull(aaActiveVenue.getDelFlag())) {
                queryWrapper.like(AaActiveVenue::getDelFlag, aaActiveVenue.getDelFlag());
            }

            if (!Objects.isNull(aaActiveVenue.getCreateBy())) {
                queryWrapper.like(AaActiveVenue::getCreateBy, aaActiveVenue.getCreateBy());
            }

            if (!Objects.isNull(aaActiveVenue.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(aaActiveVenue.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(aaActiveVenue.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(AaActiveVenue::getCreateTime, aaActiveVenue.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(aaActiveVenue.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(AaActiveVenue::getCreateTime, aaActiveVenue.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(aaActiveVenue.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(aaActiveVenue.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(AaActiveVenue::getCreateTime, aaActiveVenue.getParams().get("CreateTimeBeginTime"), aaActiveVenue.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(aaActiveVenue.getUpdateBy())) {
                queryWrapper.like(AaActiveVenue::getUpdateBy, aaActiveVenue.getUpdateBy());
            }

            if (!Objects.isNull(aaActiveVenue.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(aaActiveVenue.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(aaActiveVenue.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(AaActiveVenue::getUpdateTime, aaActiveVenue.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(aaActiveVenue.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(AaActiveVenue::getUpdateTime, aaActiveVenue.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(aaActiveVenue.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(aaActiveVenue.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(AaActiveVenue::getUpdateTime, aaActiveVenue.getParams().get("UpdateTimeBeginTime"), aaActiveVenue.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(aaActiveVenue.getRemark())) {
                queryWrapper.like(AaActiveVenue::getRemark, aaActiveVenue.getRemark());
            }
        Page<AaActiveVenue> aaActiveVenueList= aaActiveVenueMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(AaActiveVenue record:aaActiveVenueList.getRecords()){
            record.setAaActive(aaActiveService.getAaActiveById(record.getAaActiveId()));
            record.setCaVeueRentalObj(caVeueRentalObjService.getCaVeueRentalObjById(record.getCaVeueRentalObjId()));
        }

            return aaActiveVenueList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public AaActiveVenue getAaActiveVenueById(Long id) {
        if (Objects.isNull(id)) {
            return new AaActiveVenue();
        }
        try {
            AaActiveVenue aaActiveVenue = aaActiveVenueMapper.selectById(id);
            aaActiveVenue.setAaActive(aaActiveService.getAaActiveById(aaActiveVenue.getAaActiveId()));
            aaActiveVenue.setCaVeueRentalObj(caVeueRentalObjService.getCaVeueRentalObjById(aaActiveVenue.getCaVeueRentalObjId()));
            if (Objects.isNull(aaActiveVenue)){
                aaActiveVenue = new AaActiveVenue();
            }
            return aaActiveVenue;

        } catch (Exception e) {
            e.printStackTrace();
            return new AaActiveVenue();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param aaActiveVenue 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByAaActiveVenue(AaActiveVenue aaActiveVenue) {
        if (Objects.isNull(aaActiveVenue)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<AaActiveVenue> queryWrapper = new LambdaQueryWrapper<>(new AaActiveVenue());
            

            if (!Objects.isNull(aaActiveVenue.getId())) {
                queryWrapper.eq(AaActiveVenue::getId, aaActiveVenue.getId());
            }

            if (!Objects.isNull(aaActiveVenue.getAaActiveId())) {
                queryWrapper.eq(AaActiveVenue::getAaActiveId, aaActiveVenue.getAaActiveId());
            }

            if (!Objects.isNull(aaActiveVenue.getCaVeueRentalObjId())) {
                queryWrapper.eq(AaActiveVenue::getCaVeueRentalObjId, aaActiveVenue.getCaVeueRentalObjId());
            }

            if (!Objects.isNull(aaActiveVenue.getStatus())) {
                queryWrapper.like(AaActiveVenue::getStatus, aaActiveVenue.getStatus());
            }

            if (!Objects.isNull(aaActiveVenue.getDelFlag())) {
                queryWrapper.like(AaActiveVenue::getDelFlag, aaActiveVenue.getDelFlag());
            }

            if (!Objects.isNull(aaActiveVenue.getCreateBy())) {
                queryWrapper.like(AaActiveVenue::getCreateBy, aaActiveVenue.getCreateBy());
            }

            if (!Objects.isNull(aaActiveVenue.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(aaActiveVenue.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(aaActiveVenue.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(AaActiveVenue::getCreateTime, aaActiveVenue.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(aaActiveVenue.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(AaActiveVenue::getCreateTime, aaActiveVenue.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(aaActiveVenue.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(aaActiveVenue.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(AaActiveVenue::getCreateTime, aaActiveVenue.getParams().get("CreateTimeBeginTime"), aaActiveVenue.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(aaActiveVenue.getUpdateBy())) {
                queryWrapper.like(AaActiveVenue::getUpdateBy, aaActiveVenue.getUpdateBy());
            }

            if (!Objects.isNull(aaActiveVenue.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(aaActiveVenue.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(aaActiveVenue.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(AaActiveVenue::getUpdateTime, aaActiveVenue.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(aaActiveVenue.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(AaActiveVenue::getUpdateTime, aaActiveVenue.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(aaActiveVenue.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(aaActiveVenue.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(AaActiveVenue::getUpdateTime, aaActiveVenue.getParams().get("UpdateTimeBeginTime"), aaActiveVenue.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(aaActiveVenue.getRemark())) {
                queryWrapper.like(AaActiveVenue::getRemark, aaActiveVenue.getRemark());
            }

            List<AaActiveVenue> aaActiveVenues = aaActiveVenueMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (AaActiveVenue aaActiveVenueNo:aaActiveVenues){
                idList.add(aaActiveVenueNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    /**
     * 通过aaActive外键idList查询idList
     *
     * @param aaActiveIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByAaActiveIdList(List<Long> aaActiveIdList) {
        if (Objects.isNull(aaActiveIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<AaActiveVenue> queryWrapper = new LambdaQueryWrapper<>(new AaActiveVenue());
            queryWrapper.in(AaActiveVenue::getAaActiveId, aaActiveIdList);
            List<AaActiveVenue> aaActiveVenues = aaActiveVenueMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (AaActiveVenue aaActiveVenue:aaActiveVenues){
                idList.add(aaActiveVenue.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }


    /**
     * 通过caVeueRentalObj外键idList查询idList
     *
     * @param caVeueRentalObjIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByCaVeueRentalObjIdList(List<Long> caVeueRentalObjIdList) {
        if (Objects.isNull(caVeueRentalObjIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<AaActiveVenue> queryWrapper = new LambdaQueryWrapper<>(new AaActiveVenue());
            queryWrapper.in(AaActiveVenue::getCaVeueRentalObjId, caVeueRentalObjIdList);
            List<AaActiveVenue> aaActiveVenues = aaActiveVenueMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (AaActiveVenue aaActiveVenue:aaActiveVenues){
                idList.add(aaActiveVenue.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param aaActiveVenue 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertAaActiveVenue(AaActiveVenue aaActiveVenue) {
        if (Objects.isNull(aaActiveVenue)) {
            return -1;
        }
        try {
        
            //aaActiveVenue.setSpell(PinYin.getPinYin(aaActiveVenue.getName()));
            return aaActiveVenueMapper.insert(aaActiveVenue);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param aaActiveVenue 实体类
     * @return 结果
     */
    @Override
    public int updateAaActiveVenueById(AaActiveVenue aaActiveVenue) {
        if (Objects.isNull(aaActiveVenue)) {
            return -1;
        }
        try {
            //aaActiveVenue.setSpell(PinYin.getPinYin(aaActiveVenue.getName()));
            return aaActiveVenueMapper.updateById(aaActiveVenue);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteAaActiveVenueByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return aaActiveVenueMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<AaActiveVenue> getAaActiveVenueList() {
        try {
            LambdaQueryWrapper<AaActiveVenue> queryWrapper = new LambdaQueryWrapper<>(new AaActiveVenue());
            queryWrapper.isNotNull(AaActiveVenue::getId);
            return aaActiveVenueMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
